﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using iRCL_Crypt;

namespace iRCL_CryptEditor
{
    public partial class MainForm : Form
    {
        SongFile _file;
        public MainForm()
        {
            InitializeComponent();
        }

        private void buttonNew_Click(object sender, EventArgs e)
        {
            if (openNew.ShowDialog(this) != DialogResult.Cancel)
            {
                _file = new SongFile();
                _file.FileName = openNew.FileName.Substring(0, openNew.FileName.LastIndexOf('.')) + ".crypt";
                _file.Buffer = File.ReadAllBytes(openNew.FileName);
                textOriginal.Text = openNew.FileName;
                textCrypt.Text = _file.FileName;
                buttonSave.Enabled = true;
            }
        }

        private void buttonOpen_Click(object sender, EventArgs e)
        {
            if (open.ShowDialog(this) != DialogResult.Cancel)
            {
                _file = new SongFile();
                _file.FileName = open.FileName;
                _file.Load();
                textCrypt.Text = _file.FileName;
                textTrack.Text = _file.SongInfo.track.ToString();
                textDisk.Text = _file.SongInfo.disk.ToString();
                checkCompilation.Checked = _file.SongInfo.compilation;
                textAlbum.Text = _file.SongInfo.album;
                textAuthor.Text = _file.SongInfo.author;
                textTitle.Text = _file.SongInfo.title;
                textDate.Text = _file.SongInfo.date;
                textGenre.Text = _file.SongInfo.genre;
                buttonSave.Enabled = true;
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (_file.SongInfo == null)
                _file.SongInfo = new SongInfo();
            _file.SongInfo.track = UInt16.Parse(textTrack.Text);
            _file.SongInfo.disk = UInt16.Parse(textDisk.Text);
            _file.SongInfo.compilation = checkCompilation.Checked;
            _file.SongInfo.album = textAlbum.Text;
            _file.SongInfo.author = textAuthor.Text;
            _file.SongInfo.title = textTitle.Text;
            _file.SongInfo.date = textDate.Text;
            _file.SongInfo.genre = textGenre.Text;
            _file.FileName = textCrypt.Text;
            _file.Save();
        }
    }
}
